package com.hldsmart.smartda.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.model.entity.ShopEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by liuzhihui on 2017/2/9.
 *
 */

public class FootPrintAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<ShopEntity> list = new ArrayList<>();
    private FootPrintAdapter.OnItemClickedListener listener;

    public void addData(List<ShopEntity> data) {
        list.addAll(data);
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view;
        view = LayoutInflater.from(context).inflate(R.layout.rv_list_footprint_item, parent, false);
        return new FootPrintAdapter.ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

            FootPrintAdapter.ListViewHolder listViewHolder = (FootPrintAdapter.ListViewHolder) holder;
            listViewHolder.name.setText(list.get(position).getName());
            listViewHolder.type.setText(list.get(position).getType());
            listViewHolder.im.setImageResource(list.get(position).getPage());
            listViewHolder.ll_list_shop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onPhotoClicked(list.get(position));
                    }
                }
            });

//            ImageLoader.getInstance().displayImage(ShopApi.IMAGE_URL + list.get(position-2).getPage(),
//                    listViewHolder.im, ImageLoadOptions.build_item());

    }


    @Override
    public int getItemCount() {
        return list.size();
    }


    public void setListener(FootPrintAdapter.OnItemClickedListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickedListener {
        void onPhotoClicked(ShopEntity shopEntity);
    }

    class ListViewHolder extends RecyclerView.ViewHolder {


        @Bind(R.id.iv_image)
        ImageView im;

        @Bind(R.id.tv_shopName)
        TextView name;

        @Bind(R.id.tv_type)
        TextView type;
        @Bind(R.id.ll_list_shop)
        LinearLayout ll_list_shop;


        public ListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}